package org.leetcode.middle.leetcode139;

import java.util.*;

public class Solution {

    public boolean wordBreak6(String s, List<String> wordDict) {

        int length = s.length();
        int size = wordDict.size();

        boolean[] dp = new boolean[length + 1];

        Arrays.fill(dp, false);

        dp[0]=true;

        for (int i = 0; i <= length; i++) {
            for (int j = 0; j < size; j++) {
                int currSize = wordDict.get(j).length();

                if (i >=currSize && dp[i - currSize]) {
                    if (s.substring(i - currSize, i).equals(wordDict.get(j))) {
                        dp[i]=true;
                        break;
                    }
                }
            }
        }

        return dp[length];
    }

    //求排列数
    public boolean wordBreak5(String s, List<String> wordDict) {

        boolean[] dp = new boolean[s.length() + 1];

        Set<String> set = new HashSet<>(wordDict);

        dp[0] = true;

        //先遍历容量，再遍历物品
        for (int i = 0; i <= s.length(); i++) {
            for (String word : set) {
                if (i >= word.length() && dp[i - word.length()] && word.equals(s.substring(i - word.length(), i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }

    public boolean wordBreak(String s, List<String> wordDict) {

        //dp数组的含义：dp[4],表示s的下标为4之前的字符串可不可以由wordDict中的str构成.
        Set<String> set = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;

        for (int i = 0; i <= s.length(); i++) {
            for (String word : set) {
                int length = word.length();
                if (i >= length && dp[i - length] && s.substring(i - length, i).equals(word)) {
                    dp[i] = true;
                    break;
                }
            }
        }

        return dp[s.length()];
    }

    public boolean wordBreak2(String s, List<String> wordDict) {
        Set<String> wordDictSet = new HashSet(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < i; j++) {
                String temp = s.substring(j, i);
                if (dp[j] && wordDictSet.contains(temp)) {
                    dp[i] = true;
                    break;
                }
            }
        }

        return dp[s.length()];
    }


    public boolean wordBreak3(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;

        for (int i = 1; i <= s.length(); i++) {
            for (String word : wordDict) {
                int len = word.length();
                if (i >= len && dp[i - len] && word.equals(s.substring(i - len, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }

        return dp[s.length()];
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<String> wordDict = new ArrayList<>();

        wordDict.add("leet");
        wordDict.add("code");
        boolean test = solution.wordBreak3("leetcode60", wordDict);

        System.out.println(test);
    }

}
